package com.xinzhi.controller;

import com.xinzhi.anno.CheckRole;
import com.xinzhi.domain.User;
import com.xinzhi.domain.dto.ChangeProductTypeDto;
import com.xinzhi.domain.dto.ProductTypeDto;
import com.xinzhi.service.LoginService;
import com.xinzhi.service.ProductTypeService;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class ProductTypeController {

    @Autowired
    ProductTypeService productTypeService;

    @Autowired
    LoginService loginService;

    @RequestMapping("/mtyx/ProductOneType")
    Result getProductOneType(){
        return productTypeService.getProductOneType();
    }

    /**
     * 根据一级类目 id 获取对应的二级分类以及商品
     * @param oneTypeId            一级类目 id
     * @return
     */
    @RequestMapping("/mtyx/TwoTypeAndProduct")
    Result getTwoTypeAndProduct(String oneTypeId,String current){
        return productTypeService.getTwoTypeAndProduct(oneTypeId,current);
    }

    //=================================================================================
    // 获取所有商品分类  （前后台通用）
    @RequestMapping("/mtyx_admin/productType")
    Result queryProductType(){
        return productTypeService.queryProductType();
    }
    //=================================================================================
    // 添加商品类目
    @RequestMapping("/admin/addProductType")
    @CheckRole(role = {"admin"})
    Result addProductType(ProductTypeDto productTypeDto,HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productTypeService.addProductType(productTypeDto);
    }

    // 获取商品类目(父类id)
    @RequestMapping("/admin/queryProductTypeDetail")
    @CheckRole(role = {"admin"})
    Result queryProductTypeDetail(String productTypeId,HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productTypeService.queryProductTypeDetail(productTypeId);
    }

    // 修改商品类目
    @RequestMapping("/admin/updateProductType")
    @CheckRole(role = {"admin"})
    Result updateProductType(ChangeProductTypeDto changeProductTypeDto,HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productTypeService.updateProductType(changeProductTypeDto);
    }

    // 删除商品类目
    @RequestMapping("/admin/deleteProductType")
    @CheckRole(role = {"admin"})
    Result deleteProductType(String productTypeId,HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token == null) return Result.error(5010,"token无效");

        return productTypeService.deleteProductType(productTypeId);
    }


}
